package com.xmler.plugin.adbrocket.adb.command

import com.android.ddmlib.IDevice
import com.android.ddmlib.InstallException
import com.intellij.openapi.project.Project
import com.xmler.plugin.adbrocket.utils.AdbUtil
import com.xmler.plugin.adbrocket.utils.Logger
import org.jetbrains.android.facet.AndroidFacet
import javax.swing.JTextArea

class UninstallCmd: ICommand {
    override fun run(project: Project, device: IDevice, facet: AndroidFacet, packageName: String, display: JTextArea): Boolean {
        try {
            if (AdbUtil.isAppInstalld(device, packageName)) {
                val errorCode = device.uninstallPackage(packageName)
                if (errorCode == null) {
                    Logger.i("%s uninstalled on %s", packageName, device.name)
                    return true
                } else {
                    Logger.e("%s uninstall failure on %s: %s", packageName, device.name, errorCode)
                }
            }else {
                Logger.e("%s is not installed on %s", packageName, device.name)
            }
        } catch (e1: Exception) {
            Logger.e("Uninstall fail... " + e1.message)
        }
        return false
    }
}